package com.example.ff.limage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Parcelable;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;

import static android.os.Environment.isExternalStorageRemovable;

public class LargeImageView extends View {

    private String                  TAG = "LargeImageView";

    private Paint                   mPaint;
    private BitmapRegionDecoder     mDecoder;
    private BitmapFactory.Options   mOptions;
    private Rect                    mDecodeRegion;
    private static Bitmap           mThumbnailBitmap;
    private Bitmap                  mBitmap;
    private Matrix                  mMatrix;
    private float                   thumbnailWidth,thumbnailHeight;
    private float                   mPercentage;
    private float                   mThumbnailLeft,mThumbnailTop,mBitmapLeft,mBitmapTop;
    private int                     mBitmapWidth,mBitmapHeight;
    private Point                   mFocus;
    private boolean                 mScaling = false;


    private DiskLruCache            mDiskLruCache;


    public void destroyBitmaps(){
        Log.e(TAG, "destroyBitmaps: release bitmaps" );
        if (!mDecoder.isRecycled()) {
            mDecoder.recycle();
        }
        if (!mBitmap.isRecycled()){
            mBitmap.recycle();
            mBitmap = null;
        }
    }
    public void destroyThumbnailBitmap(){
        if (!mThumbnailBitmap.isRecycled()){
            mThumbnailBitmap.recycle();
            mThumbnailBitmap = null;
        }
    }
    public void createThumbnailBitmap(){
        if (null == mThumbnailBitmap){
            DecodeTask  task = new DecodeTask();
            task.execute(mDecodeRegion);
        }
    }
    public void createBitmaps(){

    }
    private class DecodeTask extends AsyncTask<Rect,Void,Void>{

        @Override
        protected Void doInBackground(Rect... rects) {
            Log.e(TAG, "doInBackground: "  );
            long begin,end;

            mDecodeRegion = new Rect(0,0,mBitmapWidth,mBitmapHeight);

            mOptions.inSampleSize = 2;

            begin = SystemClock.elapsedRealtime();
            mThumbnailBitmap = mDecoder.decodeRegion(mDecodeRegion,mOptions);
            end = SystemClock.elapsedRealtime();
            Log.e(TAG, "doInBackground: decode mThumbnailBitmap need " + (end - begin) + "  ms. "  );

            return null;
        }
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    }

    // 打印Matrix内数据
    void showMatrix(Matrix matrix,String tag){
        // 下面的代码是为了查看matrix中的元素
        float[] matrixValues = new float[9];
        matrix.getValues(matrixValues);
        String temp ;
        for (int i = 0; i < 3; ++i) {
            temp = "";
            for (int j = 0; j < 3; ++j) {
                temp += matrixValues[3 * i + j] + "\t";
            }
            Log.e(tag, temp);
        }
    }
    private void printThumbnailRect(){
        float left = mThumbnailLeft;
        float top = mThumbnailTop;
        float right = mThumbnailLeft + thumbnailWidth;
        float bottom = mThumbnailTop + thumbnailHeight;

        Log.e(TAG, "printThumbnailRect: left = " + left + " top = " + top + " right = " + right + " bottom = " + bottom
            + " width = " + thumbnailWidth + " height = " + thumbnailHeight);
    }
    private ScaleGestureDetector scaleDetector;
    private ScaleGestureDetector.SimpleOnScaleGestureListener scaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            float factor = detector.getScaleFactor();
            if (factor == 1 ) return true;
            if (mPercentage == 100 && factor > 1){
                return true;
            }
            if (thumbnailWidth * factor < mBitmapWidth){
                if (thumbnailWidth * factor >= mBitmapWidth * 0.01f){
                    thumbnailWidth *= factor;
                    thumbnailHeight *= factor;
                    mMatrix.postScale(factor,factor,detector.getFocusX(),detector.getFocusY());
                }
            }else{
                factor = mBitmapWidth / thumbnailWidth;
                mMatrix.postScale(factor,factor,detector.getFocusX(),detector.getFocusY());

                thumbnailWidth = mBitmapWidth;
                thumbnailHeight = mBitmapHeight;
                mPercentage = 100.0f;

                float matrix[] = new float[9];
                mMatrix.getValues(matrix);
                mThumbnailLeft = matrix[2];
                mThumbnailTop = matrix[5];

                float left = mThumbnailLeft;
                float top = mThumbnailTop;
                float right = mThumbnailLeft + thumbnailWidth;
                float bottom = mThumbnailTop + thumbnailHeight;

                int viewWith = getWidth();
                int viewHeight = getHeight();

                mBitmapLeft = 0;
                mBitmapTop = 0;

                if (left > 0){
                    mBitmapLeft = left;
                    mDecodeRegion.left = 0;
                    mDecodeRegion.right = (int) (viewWith - left);
                    if (top > 0){
                        mBitmapTop = top;
                        mDecodeRegion.top = 0;
                        mDecodeRegion.bottom = (int) (viewHeight - top);
                    }else if(bottom < viewHeight){
                        mDecodeRegion.bottom = mBitmapHeight;
                        mDecodeRegion.top = (int) (mBitmapHeight - bottom);
                    }else {
                        mDecodeRegion.top = (int) -top;
                        mDecodeRegion.bottom = mDecodeRegion.top + viewHeight;
                    }
                }else if(top > 0){
                    mBitmapTop = top;
                    mDecodeRegion.top = 0;
                    mDecodeRegion.bottom = (int) (viewHeight - top);
                    if (right < viewWith){
                        mDecodeRegion.right = mBitmapWidth;
                        mDecodeRegion.left = (int) (mBitmapWidth - right);
                    }else{
                        mDecodeRegion.left = (int) -left;
                        mDecodeRegion.right = mDecodeRegion.left + viewWith;
                    }
                }else if(right < viewWith ){
                    mDecodeRegion.right = mBitmapWidth;
                    mDecodeRegion.left = (int) (mBitmapWidth - right);
                    if(bottom < viewHeight){
                        mDecodeRegion.bottom = mBitmapHeight;
                        mDecodeRegion.top = (int) (mBitmapHeight - bottom);
                    }else{
                        mDecodeRegion.top = (int) -top;
                        mDecodeRegion.bottom = mDecodeRegion.top + viewHeight;
                    }
                }else if(bottom < viewHeight){
                    mDecodeRegion.left = (int) -left;
                    mDecodeRegion.right = mDecodeRegion.left + viewWith;

                    mDecodeRegion.bottom = mBitmapHeight;
                    mDecodeRegion.top = (int) (mBitmapHeight - bottom);
                }else{
                    mDecodeRegion.left = (int) -left;
                    mDecodeRegion.right = mDecodeRegion.left + viewWith;
                    mDecodeRegion.top = (int) -top;
                    mDecodeRegion.bottom = mDecodeRegion.top + viewHeight;
                }
                mOptions.inSampleSize = 1;

                if (mDecodeRegion.width() > 0 && mDecodeRegion.height() > 0){
                    mBitmap = mDecoder.decodeRegion(mDecodeRegion,mOptions);
                }else{
                    if (mBitmap != null && !mBitmap.isRecycled()){
                        mBitmap.recycle();
                        mBitmap = null;
                    }
                }

            }
            BigDecimal bd = new BigDecimal(thumbnailWidth * 1.0f / mBitmapWidth * 100).setScale(1,BigDecimal.ROUND_HALF_UP);
            mPercentage = bd.floatValue();
            Log.i(TAG, "onScale: bitmap.w = " + mThumbnailBitmap.getWidth() + " bitmap.h = " + mThumbnailBitmap.getHeight()
                    + " thumbnailWidth = " + thumbnailWidth + " thumbnailHeight = " + thumbnailHeight);

            invalidate();
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {

            mScaling = true;

            float focusX = detector.getFocusX();
            float focusY = detector.getFocusY();
            mFocus = new Point((int)focusX,(int)focusY);

            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            mScaling = false;
            mFocus = null;

            super.onScaleEnd(detector);
        }
    };

    private GestureDetectorCompat scrollDetector;
    private GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

            if (mScaling || Math.abs(distanceX) < 5 && Math.abs(distanceY) < 5) return true;

            float matrix[] = new float[9];
            mMatrix.getValues(matrix);
            mThumbnailLeft = matrix[2];
            mThumbnailTop = matrix[5];
            int viewWith = getWidth();
            int viewHeight = getHeight();

            if (mThumbnailLeft <= viewWith && mThumbnailLeft >= -thumbnailWidth
                    && mThumbnailTop <= viewHeight && mThumbnailTop >= -thumbnailHeight){
                if (mThumbnailLeft > 0 && viewWith - mThumbnailLeft < -distanceX){
                    distanceX = -(viewWith - mThumbnailLeft);
                }else if(mThumbnailLeft < 0 && mThumbnailLeft + thumbnailWidth < distanceX){
                    distanceX = mThumbnailLeft + thumbnailWidth;
                }
                if (mThumbnailTop > 0 && viewHeight - mThumbnailTop < -distanceY){
                    distanceY = -(viewHeight - mThumbnailTop);
                }else if(mThumbnailTop < 0 && mThumbnailTop + thumbnailHeight < distanceY){
                    distanceY = mThumbnailTop + thumbnailHeight;
                }

                mMatrix.postTranslate(-distanceX,-distanceY);


                if (mPercentage == 100.0f){
                    mMatrix.getValues(matrix);
                    mThumbnailLeft = matrix[2];
                    mThumbnailTop = matrix[5];

                    float left = mThumbnailLeft;
                    float top = mThumbnailTop;
                    float right = mThumbnailLeft + thumbnailWidth;
                    float bottom = mThumbnailTop + thumbnailHeight;

                    mBitmapLeft = 0;
                    mBitmapTop = 0;

                    if (left > 0){
                        mBitmapLeft = left;
                        mDecodeRegion.left = 0;
                        mDecodeRegion.right = (int) (viewWith - left);
                        if (top > 0){
                            mBitmapTop = top;
                            mDecodeRegion.top = 0;
                            mDecodeRegion.bottom = (int) (viewHeight - top);
                        }else if(bottom < viewHeight){
                            mDecodeRegion.bottom = mBitmapHeight;
                            mDecodeRegion.top = (int) (mBitmapHeight - bottom);
                        }else {
                            mDecodeRegion.top = (int) -top;
                            mDecodeRegion.bottom = mDecodeRegion.top + viewHeight;
                        }
                    }else if(top > 0){
                        mBitmapTop = top;
                        mDecodeRegion.top = 0;
                        mDecodeRegion.bottom = (int) (viewHeight - top);
                        if (right < viewWith){
                            mDecodeRegion.right = mBitmapWidth;
                            mDecodeRegion.left = (int) (mBitmapWidth - right);
                        }else{
                            mDecodeRegion.left = (int) -left;
                            mDecodeRegion.right = mDecodeRegion.left + viewWith;
                        }
                    }else if(right < viewWith ){
                        mDecodeRegion.right = mBitmapWidth;
                        mDecodeRegion.left = (int) (mBitmapWidth - right);
                        if(bottom < viewHeight){
                            mDecodeRegion.bottom = mBitmapHeight;
                            mDecodeRegion.top = (int) (mBitmapHeight - bottom);
                        }else{
                            mDecodeRegion.top = (int) -top;
                            mDecodeRegion.bottom = mDecodeRegion.top + viewHeight;
                        }
                    }else if(bottom < viewHeight){
                        mDecodeRegion.left = (int) -left;
                        mDecodeRegion.right = mDecodeRegion.left + viewWith;

                        mDecodeRegion.bottom = mBitmapHeight;
                        mDecodeRegion.top = (int) (mBitmapHeight - bottom);
                    }else{
                        mDecodeRegion.left = (int) -left;
                        mDecodeRegion.right = mDecodeRegion.left + viewWith;
                        mDecodeRegion.top = (int) -top;
                        mDecodeRegion.bottom = mDecodeRegion.top + viewHeight;
                    }
                    mOptions.inSampleSize = 1;

                    Log.e(TAG, "onScroll: mDecodeRegion = " + mDecodeRegion );
                    if (mDecodeRegion.width() > 0 && mDecodeRegion.height() > 0){
                        mBitmap = mDecoder.decodeRegion(mDecodeRegion,mOptions);
                    }else{
                        mBitmap = null;
                    }
                }
                invalidate();
            }
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return true;
        }
    };
    private void initGestureDetector() {
        scrollDetector = new GestureDetectorCompat(getContext(), simpleOnGestureListener);
        scaleDetector = new ScaleGestureDetector(getContext(), scaleGestureListener);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean ret = scaleDetector.onTouchEvent(event);
        ret |= scrollDetector.onTouchEvent(event);
        return ret || true;
    }
    private static final int DISK_CACHE_SIZE = 1024 * 1024 * 256; // 256MB
    private static final String DISK_CACHE_SUBDIR = "thumbnails";

    void initCache(){

        try {
            Context context = getContext();
            File cacheDir = context.getCacheDir();

            cacheDir = context.getExternalCacheDir();

            final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||!isExternalStorageRemovable()
                    ? context.getExternalCacheDir().getPath()
                    : context.getCacheDir().getPath();
            cacheDir = new File(cachePath + File.separator + DISK_CACHE_SUBDIR);

            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);

            int appVersion = pi.versionCode;
            int valueCount = 1;

            mDiskLruCache = DiskLruCache.open(cacheDir, appVersion,valueCount,DISK_CACHE_SIZE);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }
    void init(){
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(24);
        setLayerType(View.LAYER_TYPE_SOFTWARE,mPaint);

//        LinearGradient backGradient = new LinearGradient(0, 0, 0, mPaint.getTextSize(), new int[]{Color.BLACK, Color.GRAY ,Color.YELLOW}, null, Shader.TileMode.CLAMP);
//        mPaint.setShader(backGradient);

        mMatrix = new Matrix();

//        initCache();

        initGestureDetector();

        initRegionDecoder();

    }
    void initRegionDecoder(){
        try {
            InputStream fs = getResources().getAssets().open("world_map.jpg");
            mDecoder = BitmapRegionDecoder.newInstance(fs,false);
            mBitmapWidth = mDecoder.getWidth();
            mBitmapHeight = mDecoder.getHeight();

            mOptions = new BitmapFactory.Options();
            mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
            mOptions.inSampleSize = 1;

        } catch (IOException e) {

            e.printStackTrace();
        }

    }
    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LargeImageViewAttrs);
        String file = ta.getString(R.styleable.LargeImageViewAttrs_src);
        ta.recycle();
    }


    public LargeImageView(Context context) {
        super(context);
        init();
    }

    public LargeImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context,attrs);
        init();
    }

    public LargeImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context,attrs);
        init();
    }

    @SuppressLint("NewApi")
    public LargeImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        getAttrs(context,attrs);
        init();
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        super.onRestoreInstanceState(state);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    public static int calculateInSampleSize(int width, int height, int reqWidth, int reqHeight) {
        // Raw height and width of image
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            if (halfHeight <= 0 || halfWidth <= 0) return inSampleSize;

            if (width > height) {
                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfWidth / inSampleSize) >= reqWidth) {
                    inSampleSize *= 2;
                }
            } else {
                while ((halfHeight / inSampleSize) >= reqHeight) {
                    inSampleSize *= 2;
                }
            }
        }
        return inSampleSize;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (mPercentage < 100.0f && mThumbnailBitmap != null){
            canvas.drawBitmap(mThumbnailBitmap,mMatrix,mPaint);
        }else if(mPercentage == 100.0f && mBitmap != null){
            canvas.drawBitmap(mBitmap,mBitmapLeft,mBitmapTop,mPaint);
        }
        if (mFocus != null){
            mPaint.setColor(Color.RED);
            mPaint.setMaskFilter(new BlurMaskFilter(16, BlurMaskFilter.Blur.SOLID));
            mPaint.setColor(Color.parseColor("#ff0000"));
            canvas.drawCircle(mFocus.x,mFocus.y,16,mPaint);
        }

        //draw percentage
        String percentage = mPercentage + "%";

        float textSize = mPaint.getTextSize();
        float percentWidth = mPaint.measureText("100.0%");

        float circleX = getWidth() - percentWidth  ;
        float circleY = percentWidth  ;

        mPaint.setColor(Color.parseColor("#7f7A378B"));
        mPaint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));
        canvas.drawCircle(circleX ,circleY,percentWidth * 0.66f,mPaint);

        mPaint.setColor(Color.WHITE);
        mPaint.setMaskFilter(null);
        percentWidth = mPaint.measureText(percentage);
        circleY += textSize / 2;
        circleX -= percentWidth / 2;
        canvas.drawText(percentage,circleX ,circleY,mPaint);

    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        Log.e(TAG, "onMeasure: " );

        int viewWidth = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();

        if (viewWidth == 0) viewWidth = 56;

        viewWidth = resolveSizeAndState(viewWidth, widthMeasureSpec, 0);

        int viewHeight = getSuggestedMinimumHeight() + getPaddingBottom() + getPaddingTop();

        if (viewHeight == 0) viewHeight = 56;

        //计算最佳值,在其中解析了 heightMeasureSpec
        viewHeight = resolveSizeAndState(viewHeight, heightMeasureSpec, 0);

        //将量算的结果保存到View的成员变量mMeasuredWidth 和mMeasuredHeight中。
        setMeasuredDimension(viewWidth, viewHeight);

        // 量算完成之后，View的父控件就可以通过调用
        // getMeasuredWidth、getMeasuredState、getMeasuredWidthAndState
        // 这三个方法获取View的量算结果。


        mBitmapLeft = 0;
        mBitmapTop = 0;

        int left = mBitmapWidth / 2 - viewWidth / 2;
        int top = mBitmapHeight / 2 - viewHeight / 2;

        mDecodeRegion = new Rect(left,top,left + viewWidth ,top + viewHeight );
//        mDecodeRegion = new Rect(0,0,mBitmapWidth ,mBitmapHeight);

        mOptions.inSampleSize = calculateInSampleSize(mDecodeRegion.width(),mDecodeRegion.height(),viewWidth,viewHeight);
//        mOptions.inSampleSize = 2;

        long begin,end;
        begin = SystemClock.elapsedRealtime();
        mBitmap = mDecoder.decodeRegion(mDecodeRegion,mOptions);
        end = SystemClock.elapsedRealtime();

        thumbnailWidth = mBitmapWidth;
        thumbnailHeight = mBitmapHeight;

        BigDecimal bd = new BigDecimal(thumbnailWidth * 1.0f / mBitmapWidth * 100).setScale(1,BigDecimal.ROUND_HALF_UP);
        mPercentage = bd.floatValue();

        Log.e(TAG, "init region = " + mDecodeRegion + " ratio = " + (float) mDecodeRegion.width() / mDecodeRegion.height()
                + " bitmap.w = " + mBitmap.getWidth() + " bitmap.h = " + mBitmap.getHeight() + " ratio = " + (float) mBitmap.getWidth() / mBitmap.getHeight()
                + " need " + (end - begin) + " ms");
        if (mThumbnailBitmap != null){
            Log.e(TAG, "onMeasure : mThumbnailBitmap is ok.");

            float width = mThumbnailBitmap.getWidth();
            float height = mThumbnailBitmap.getHeight();
            float dx = viewWidth / 2 - width / 2;
            float dy = viewHeight / 2 - height / 2;
            mMatrix.setTranslate(dx,dy);

            float factor = mBitmapWidth / width;
            mMatrix.postScale(factor,factor,viewWidth/ 2,viewHeight /2);

        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Log.e(TAG, "onFinishInflate: " );
        createThumbnailBitmap();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.e(TAG, "onAttachedToWindow: " );
    }
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.e(TAG, "onDetachedFromWindow: " );

    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        Log.e(TAG, "onWindowVisibilityChanged : "  + visibility);

    }
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        Log.e(TAG, "onWindowFocusChanged:  hasWindowFocus = " + hasWindowFocus );
    }

    Bitmap loadBitmapFromCache(String key){
        Bitmap bitmap = null;
        try {
            DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
            if (snapshot != null) {
                InputStream is = snapshot.getInputStream(0);
                bitmap = BitmapFactory.decodeStream(is);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
    void pushToCache(String key, Bitmap value){
        try {
            DiskLruCache.Editor editor = mDiskLruCache.edit(key);

            if (editor != null){
                OutputStream out = editor.newOutputStream(0);
                value.compress(Bitmap.CompressFormat.JPEG, 100, out);
                out.flush();
                editor.commit();
                mDiskLruCache.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
